import java.util.ArrayDeque;
import java.util.Deque;

/**
 * Created by asus on 2017/10/18.
 */
public class Codes {
    public static void main(String[] args) {
        int [] key = {5,12,-3,8,-9,4,10};
        Integer keyValue;
        String encoded = "",decoded="";
        String messag = "All programmers are playwrights and all"+"computers are lousy actors.";
        Deque<Integer> keyQueue1 = new ArrayDeque<>();
        Deque<Integer> keyQueue2 = new ArrayDeque<>();
        for (int scan=0;scan<key.length;scan++){
            keyQueue1.add(new Integer(key[scan]));
            keyQueue2.add(new Integer(key[scan]));
        }
        for (int scan=0;scan<messag.length();scan++){
            keyValue = keyQueue1.pollFirst();
            encoded+=(char)((int)messag.charAt(scan)+keyValue.intValue());
            keyQueue1.add(keyValue);
        }
        System.out.println("Encoded Message:\n"+encoded+"\n");
        for (int scan=0;scan<encoded.length();scan++){
            keyValue = keyQueue2.pollFirst();
            decoded+=(char)((int)encoded.charAt(scan)-keyValue.intValue());
            keyQueue2.add(keyValue);
        }
        System.out.println("Decoded Message:\n"+decoded);
      }

}
